#

| Chapter 44 Shell Programming for the Uninitiated |

## 44.19 The Bourne Shell set Command

[Most of this article, except IFS and `--`, also applies to the C shell. -JP]

The Bourne shell command line can have options like -e (exit if any command returns non-zero status). It can also have other arguments; these are passed to shell scripts. You can set new command-line parameters while you're typing interactive commands (at a shell prompt) or in a shell script.

To reset the command-line parameters, just type set followed by the new parameters. So, for example, to ask the shell to show expanded versions of command lines after you type them, set the -v (verbose) option (8.17):

>     $ set -v
>     $ mail $group1 < message
>     mail andy ellen heather steve wilma < message
>     $ mail $group2 < message
>     mail jpeek@jpeek.com randy@xyz.edu yori@mongo.medfly.com < message
>     $ set +v

Typing `set +v` cancels the v option on many Bourne shells.

You can put filenames or any other strings in the command-line parameters interactively or from a shell script. That's handy for storing and parsing the output of a UNIX command with backquotes (9.16). For example, you can get a list of all logged-in users from the parameters `$1`, `$2`, and so on. Use users if your system has it. Otherwise, use who (51.4)- and cut (35.14) to strip off everything but the usernames:

    for
    $ set `users`
    $ set `who | cut -c1-8`
    $ for u
    > do
    > ...do something with each user ($u)...
    > done

You can save the original parameters in another variable and reset them later:

>     oldparms="$*"
>     set something new
>         ...use new settings...
>     set $oldparms

If the first parameter you set starts with a dash, like `-e`, the shell will treat it as its own option instead of as a string to put into the command-line parameters. To avoid this, use `--` (two dashes) as the first argument to set. In this example, `$1` gets -e, and the filenames expanded from the wildcard pattern go into `$2`, `$3`, etc.:

>     set -- -e file*

Because the shell parses and scans the new parameters before it stores them, wildcards (15.2) and other special characters (8.19) will be interpreted - watch your quoting (8.14). You can take advantage of this to parse lines of text into pieces that aren't separated with the usual spaces and TABs - for instance, a line from a database with colon-separated fields - by setting the IFS (35.21) variable before the set command.

If you want to save any special quoting on the original command line, be careful; the quoting will be lost unless you're clever. For example, if `$1` used to be John Smith, it'll be split after it's restored: `$1` will have John and `$2` will be Smith. A better solution might be to use a subshell (13.7) for the part of the script where you need to reset the command-line parameters:

>     # reset command-line parameters during subshell only:
>     (set some new parameters
>        ...do something with new parameters...
>     )
>     # original parameters aren't affected from here on...

One last note: set won't set `$0`, the name of the script file.

- JP

44.18 Standard Command-Line Parsing |  | 44.20 test: Testing Files and Strings